    //
//  OccasionView.m
//  iFlower
//
//  Created by Hoang Thuc on 15/07/2010.
//  Copyright 2010 iNIL. All rights reserved.
//

#import "OccasionView.h"
#import "ProductView.h"
#import "iFlowerAppDelegate.h"
#import "MyAccountView.h"
#import "Request.h"
#import "RegisterView.h"
#import "LoginView.h"

@implementation OccasionView

@synthesize btnRegister;
@synthesize categories = _categories;
@synthesize tablelView;
-(IBAction) button1Pressed
{
	ProductView * product = [[ProductView alloc] initWithNibName:@"ProductView" bundle:nil];
	product.catId = 3;
	product.titleText = @"Anniversary";
	[self.navigationController pushViewController:product animated:YES];
	[product release];
}
-(IBAction) button2Pressed
{
	ProductView * product = [[ProductView alloc] initWithNibName:@"ProductView" bundle:nil];
	product.catId = 103;
	product.titleText = @"Birthday";
	[self.navigationController pushViewController:product animated:YES];
	[product release];
}
-(IBAction) button3Pressed
{
	ProductView * product = [[ProductView alloc] initWithNibName:@"ProductView" bundle:nil];
	product.catId = 258;
	product.titleText = @"Get Well";
	[self.navigationController pushViewController:product animated:YES];
	[product release];
}
-(IBAction) button4Pressed
{
	ProductView * product = [[ProductView alloc] initWithNibName:@"ProductView" bundle:nil];
	product.catId = 444;
	product.titleText = @"Thank you";
	[self.navigationController pushViewController:product animated:YES];
	[product release];
}
-(IBAction) button5Pressed
{
	ProductView * product = [[ProductView alloc] initWithNibName:@"ProductView" bundle:nil];
	product.catId = 253;
	product.titleText = @"New baby";
	[self.navigationController pushViewController:product animated:YES];
	[product release];
}
-(IBAction) button6Pressed
{
	ProductView * product = [[ProductView alloc] initWithNibName:@"ProductView" bundle:nil];
	product.catId = 445;
	product.titleText = @"Congratulations";
	[self.navigationController pushViewController:product animated:YES];
	[product release];
}
-(IBAction) button7Pressed
{
	ProductView * product = [[ProductView alloc] initWithNibName:@"ProductView" bundle:nil];
	product.catId = 447;
	product.titleText = @"Sympathy & Funerals";
	[self.navigationController pushViewController:product animated:YES];
	[product release];
}
-(IBAction) button8Pressed
{
	ProductView * product = [[ProductView alloc] initWithNibName:@"ProductView" bundle:nil];
	product.catId = 257;
	product.titleText = @"Romantic Gifts";
	[self.navigationController pushViewController:product animated:YES];
	[product release];
}
-(IBAction) button9Pressed
{
	ProductView * product = [[ProductView alloc] initWithNibName:@"ProductView" bundle:nil];
	product.catId = 465;
	product.titleText = @"Valentine";
	[self.navigationController pushViewController:product animated:YES];
	[product release];
}
-(IBAction) buttonMenu
{
	[self.navigationController popViewControllerAnimated:YES];
}
-(IBAction) buttonMyAccount
{
	if ([[iFlowerAppDelegate shareDelegate] logined]) {
		MyAccountView * account = [[MyAccountView alloc] initWithNibName:@"MyAccountView" bundle:nil];
		[self.navigationController pushViewController:account animated:YES];
		[account release];
	}
	else {
		LoginView * login = [[LoginView alloc] initWithNibName:@"LoginView" bundle:nil];
		[self.navigationController pushViewController:login animated:YES];
		[login release];		
	}
}
-(IBAction) buttonRegister
{
	RegisterView * registerView = [[RegisterView alloc] initWithNibName:@"RegisterView" bundle:nil];
	[self.navigationController pushViewController:registerView animated:YES];
	[registerView release];
}


/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

- (NSString *)flattenHTML:(NSString *)html {
	
    NSScanner *theScanner;
    NSString *text = nil;
	
    theScanner = [NSScanner scannerWithString:html];
	
    while ([theScanner isAtEnd] == NO) {
		
        // find start of tag
        [theScanner scanUpToString:@"<" intoString:NULL] ; 
		
        // find end of tag
        [theScanner scanUpToString:@">" intoString:&text] ;
		
        // replace the found tag with a space
        //(you can filter multi-spaces out later if you wish)
        html = [html stringByReplacingOccurrencesOfString:
				[ NSString stringWithFormat:@"%@>", text]
											   withString:@" "];
		
    } // while //
    
    return [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
	
}
- (void) contentLoaded:(NSNotification*) notification
{
	NSLog(@"content loaded");
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	_categories = [[NSMutableArray alloc] initWithCapacity:[[[Request sharedRequest] entries] count]];
    for (NSDictionary* dictionary in [[Request sharedRequest] entries])
	{
        [_categories addObject: dictionary];
	}
    //NSLog(@"%@",_categories);
    [self.tablelView reloadData];
    NSLog(@"LOAD OK");
    iFlowerAppDelegate *appDelegate = [iFlowerAppDelegate shareDelegate];
	[appDelegate hideLoadingView];
	//[_tableview reloadData];
}
- (void) contentFailed:(NSNotification*) notification
{
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	NSLog(@"content failed");
    iFlowerAppDelegate *appDelegate = [iFlowerAppDelegate shareDelegate];
	[appDelegate hideLoadingView];
}


#pragma mark -
#pragma mark View lifecycle
- (void) viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	if([[iFlowerAppDelegate shareDelegate] logined])
	{
		self.btnRegister.enabled = NO;
	}else {
		self.btnRegister.enabled = YES;
	}
   
}
- (void)viewDidLoad {
    [super viewDidLoad];
	tablelView.backgroundColor = [UIColor clearColor];
    tablelView.opaque = NO;
    tablelView.backgroundView = nil;
    
    [[iFlowerAppDelegate shareDelegate] showLoadingView];
    
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(contentLoaded:) name: @"ContentLoaded" object: nil];
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(contentFailed:) name: @"ContentFailed" object: nil];
	
	[[Request sharedRequest] getCategoryList];
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    self.btnRegister = nil;
}


- (void)dealloc {
	[btnRegister release];
    [tablelView release];
    [_categories release];
    [super dealloc];
}

#pragma TableView Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
    return [_categories count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 37;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
	
	UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
	}
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSDictionary *object = [_categories objectAtIndex:indexPath.row];
    
    
    //[btn setTitle:[object objectForKey:@"category"] forState:UIControlStateNormal];
   
    UILabel *lblTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, -5, 200, 37)];
    lblTitle.text = [object objectForKey:@"category"];
    [lblTitle setFont:[UIFont fontWithName:@"Helvetica" size:14]];
    [lblTitle setBackgroundColor:[UIColor clearColor]];
    lblTitle.opaque = NO;
    lblTitle.textAlignment = UITextAlignmentCenter;
    lblTitle.textColor = [UIColor whiteColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(55, 2, 207, 39)];
    [btn setBackgroundImage:[UIImage imageNamed:@"buttonbg.png"]  forState:UIControlStateNormal];
    [btn addSubview:lblTitle];
    btn.tag = indexPath.row;
    [btn setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
    btn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [btn addTarget:self action:@selector(btnPress:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:btn];
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%d",indexPath.row);
    /*
    AgendaDetailsViewController *advc = [[[AgendaDetailsViewController alloc] initWithNibName:@"AgendaDetailsViewController" 
                                                                                       bundle:nil] autorelease];
    [advc setDataSource:[self matchResultAtIndexPath:indexPath]];
    //NSLog(@"%@",[self matchResultAtIndexPath:indexPath]);
    [self.navigationController pushViewController:advc animated:YES];
    
    refreshUIWhenVisible = NO;
     */
}

- (void)btnPress:(UIButton*)sender {
    int index = sender.tag;
   
    NSDictionary *object = [_categories objectAtIndex:index];
     NSLog(@"%d",[[object objectForKey:@"id"] intValue]);
    ProductView * product = [[ProductView alloc] initWithNibName:@"ProductView" bundle:nil];
	product.catId = [[object objectForKey:@"id"] intValue];
	product.titleText = [object objectForKey:@"category"];
	[self.navigationController pushViewController:product animated:YES];
	[product release];
}
@end
